package utilPackage;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Iterator;

import utilPackage.Commons;

import main.Sprite;

public class Explosion extends Sprite implements Commons{
	
	private int lifetime = 30; //determines how far out the explosion goes
	private int originalLifetime;
	private int numberOfCircles = 4;
	private int iterations;
	private int i = 0;
	private Color myColor;
	public ArrayList move_history;
	private boolean hasTail = false;
	private boolean popyFlagToBeRenamed;
	private int popyIntToberendamed;
	
	public Explosion() {
		super();
		this.x = 0;
		this.y = 0;
		move_history = new ArrayList();
		//Sprite s = new Sprite();
		/*s.setX(x);
		s.setY( y);
		s.setLife(30);
		move_history.add(s);*/
		
	}
	
	public Explosion(int x, int y, int dx, int dy, int lifetime) {
		super();
		this.x = x;
		this.y = y;
		this.dx = dx;
		this.dy = dy;
		this.lifetime = lifetime;
		originalLifetime = lifetime;
		myColor = Color.ORANGE;
		move_history = new ArrayList();
		/*Sprite s = new Sprite();
		s.setX(x);
		s.setY( y);
		s.setLife(30);*/
		hasTail = true;
		//move_history.add(s);
		collidable = false;
	}
	
	public Explosion(int x, int y, int dx, int dy, int lifetime, Color theColor, boolean tail) {
		super();
		this.x = x;
		this.y = y;
		this.dx = dx;
		this.dy = dy;
		this.lifetime = lifetime;
		originalLifetime = lifetime;
		myColor = theColor;
		move_history = new ArrayList();
		/*Sprite s = new Sprite();
		s.setX(x);
		s.setY( y);
		s.setLife(30);
		move_history.add(s);*/
		hasTail = tail;
		collidable = false;
	}
	
	public int getLifetime() {
		return lifetime;
	}
	
	public void setLifetime(int lifetime) {
		this.lifetime = lifetime;
	}
	
	public int getNumberOfCircles() {
		return numberOfCircles;
	}
	
	public void setNumberOfCircles(int numberOfCircles) {
		this.numberOfCircles = numberOfCircles;
	}

	public int getIterations() {
		return iterations;
	}

	public void setIterations(int iterations) {
		this.iterations = iterations;
	}

	public void setMyColor(Color myColor) {
		this.myColor = myColor;
	}

	public Color getMyColor() {
		return myColor;
	}
	
	public boolean isHasTail() {
		return hasTail;
	}

	public void setHasTail(boolean hasTail) {
		this.hasTail = hasTail;
	}
	
	public void think(){
		if(lifetime <= 0) {
			die();
		}
		//if(i++%3 == 0) {
			setX(getX() + getDx());
			setY(getY() + getDy());
			setLifetime(getLifetime() - 1);
		//}
	}
	
	public void draw(){
		Graphics2D g2d = (Graphics2D)g;
		g2d.setColor(getMyColor());
		if(isHasTail()) {
			g2d.fill(new Ellipse2D.Double(getX() + getDx(), getY() + getDy(), 
				(RADIUS * (2+(2*(originalLifetime - lifetime)/originalLifetime))), 
				(RADIUS * (2+(2*(originalLifetime - lifetime)/originalLifetime)))));
		}
		else {
			//g2d.fill(new Ellipse2D.Double(getX() + getDx(), getY() + getDy(), 
				//	RADIUS * 2, RADIUS * 2));
			g2d.fill(new Ellipse2D.Double(getX() + getDx(), getY() + getDy(), 
					(RADIUS * (2+(2*(originalLifetime - lifetime)/originalLifetime))), 
					(RADIUS * (2+(2*(originalLifetime - lifetime)/originalLifetime)))));
		}
		if(isHasTail()) {
			if(getMyColor() != Color.ORANGE) {
				g2d.setColor(new Color(getMyColor().getRed(), getMyColor().getGreen(), getMyColor().getBlue(), 127));
			}
			else {
				g2d.setColor(new Color(255, 165, 0, 127));
			}
			g2d.fill(new Ellipse2D.Double(getX() - getDx(), getY() - getDy(),
					(RADIUS * (2+(3*(originalLifetime - lifetime)/originalLifetime))),
					(RADIUS * (2+(3*(originalLifetime - lifetime)/originalLifetime)))));
			g2d.setColor(new Color(255, 165, 0, 63));
			g2d.fill(new Ellipse2D.Double(getX() - getDx() - getDx(), getY() - getDy() - getDy(),
					(RADIUS * (2+(3*(originalLifetime - lifetime)/originalLifetime))),
					(RADIUS * (2+(3*(originalLifetime - lifetime)/originalLifetime)))));
			if((int)(originalLifetime * 0.875) == lifetime || popyFlagToBeRenamed) {
				if(!popyFlagToBeRenamed){
					popyFlagToBeRenamed = true;
					popyIntToberendamed = originalLifetime - lifetime;
				}
				if(getMyColor() != Color.ORANGE) {
					g2d.setColor(new Color(getMyColor().getRed(), getMyColor().getGreen(), getMyColor().getBlue(), 63));
				}
				else {
					g2d.setColor(new Color(255, 165, 0, 63));
				}
				g2d.fill(new Ellipse2D.Double(getX() - (popyIntToberendamed * getDx()),
						getY() - (popyIntToberendamed * getDy()), 
						(RADIUS * (3+(3*(originalLifetime - lifetime)/originalLifetime))),
						(RADIUS * (3+(3*(originalLifetime - lifetime)/originalLifetime)))));
			}
		}
		
	}

	@Override
	public boolean doesCollide(Sprite s) {
		// TODO Auto-generated method stub
		return false;
	}

}
